from manim import *
class CustomCurve(VMobject):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        # 定义自定义曲线的控制点
        points = [
            [25,5,0],
            [20,0,0],
            [25,-5,0]
            
        ]
        self.set_points_smoothly(points)

class CustomCurve1(VMobject):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        # 定义自定义曲线的控制点
        points = [
            [-10,-20,0],
            [-14,-25,0],
            [-10,-30,0]
            
        ]
        self.set_points_smoothly(points)
class CustomCurve2(VMobject):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        # 定义自定义曲线的控制点
        points = [
            [-5,-12.5,0],
            [10,-15,0],
            [5,-25,0]
            
        ]
        self.set_points_smoothly(points)
class CustomCurve3(VMobject):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        # 定义自定义曲线的控制点
        points = [
            [-10,-35,0],
            [-14,-40,0],
            [-10,-45,0]
            
        ]
        self.set_points_smoothly(points)


class math1(MovingCameraScene):
    def construct(self):
        self.camera.background_color = BLACK  # 设置背景颜色
        self.camera.frame_width = 100  # 设置边框宽度
        self.camera.frame_height = 60  # 设置边框高度
        self.camera.pixel_height = 1080  # 设置像素高度
        self.camera.pixel_width = 1920  # 设置像素宽度
        self.camera.center = ORIGIN  # 设置中心点位置
        self.camera.scale_factor = 1.0  # 设置缩放因子
        #设置横线
        for i in range(60*2+1):       
            dot1=Dot([-500,5*(i-60),0]).set_opacity(0.5)
            dot2=Dot([500,5*(i-60),0]).set_opacity(0.5)
            if i==6:
                line1=Line(dot1,dot2).set_color(WHITE).set_opacity(0.5)
                
            else:
                line1=Line(dot1,dot2).set_color(WHITE).set_opacity(0.5)
               
            self.add(dot1,dot2,line1)
        #设置竖线
        for i in range(100*2+1):        
            dot3=Dot([(i-100)*5,-300,0]).set_opacity(0.5)
            dot4=Dot([(i-100)*5,300,0]).set_opacity(0.5)
            if i==10:
                line2=Line(dot3,dot4).set_color(WHITE).set_opacity(0.5)
                
            else:
                line2=Line(dot3,dot4).set_color(WHITE).set_opacity(0.5)
            self.add(dot3,dot4,line2)
        #设置三个点
        dot1 = Dot(radius=1, color=RED)  
        dot1.move_to([-47.5,27.5,0]) 
        dot2 = Dot(radius=1, color=YELLOW)  
        dot2.move_to([-42.5,27.5,0])  
        dot3 = Dot(radius=1, color=GREEN)  
        dot3.move_to([-37.5,27.5,0])
        
        #镜头跟进效果 
        self.camera.frame.save_state()

        

        text = Tex(r"\text{(2010)求}$u=xy+2yz$\text{在约束条件}$x^{2}+y^{2}+z^{2}=10$\text{下的最值}"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,20,0])
        self.play(Write(text))
        
        self.play(FadeIn(dot1),run_time=0.1)
        self.play(FadeIn(dot2),run_time=0.1)
        self.play(FadeIn(dot3),run_time=0.1)

        text1 = Tex(r"\text{令}$L\left( x,y,z,\lambda \right) =xy+2yz+\lambda \left( x^{2}+y^{2}+z^{2}-10\right) $",color=BLUE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,13,0])
        self.play(Write(text1))


        text43 = Tex(
                    r"$L_{x}=y+2\lambda x=0$",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-30,6,0])
        text44 = Tex(
                    r"$L_{y}=x+2z+2\lambda y=0$",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-30,0,0])
        text45 = Tex(
                    r"$L_{z}=2y+2\lambda z=0$",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-30,-6,0])
        text46 = Tex(
                    r"$L_{\lambda }=x^{2}+y^{2}+z^{2}=10$",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-30,-12,0])
        text430 = Tex(
                    r"$1.$",color=WHITE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text43,LEFT,buff=1)
        text440 = Tex(
                    r"$2.$",color=WHITE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text44,LEFT,buff=1)
        text450 = Tex(
                    r"$3.$",color=WHITE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text45,LEFT,buff=1)
        text460 = Tex(
                    r"$4.$",color=WHITE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text46,LEFT,buff=1)
        self.play(Create(text43),Create(text44),Create(text45),Create(text46),
                  Create(text430),Create(text440),Create(text450),Create(text460))
        rectangle7=Rectangle(color=BLUE,fill_opacity=0.0,width=32,height=16
                            ,stroke_color=BLUE,stroke_width=20)
        rectangle7.move_to([-30,0,0])
        self.play(Create(rectangle7))
        text47 = Tex(
                    r"\text{含参方程}",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(rectangle7,UP,buff=1)
        self.play(Create(text47))
        rectangle8=Rectangle(color=BLUE,fill_opacity=0.0,width=32,height=5
                            ,stroke_color=PURPLE,stroke_width=20)
        rectangle8.move_to([-30,-12,0])
        self.play(Create(rectangle8))
        text48 = Tex(
                    r"\text{无参方程}",color=WHITE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(rectangle8,DOWN,buff=1)
        self.play(Create(text48))
        text480 = Tex(
                    r"\text{无参方程(还需2个)}",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(rectangle8,DOWN,buff=1)
        self.play(
            ReplacementTransform(text48,text480)
        )
        self.wait(0.5)


        #箭头引出
        arrow5 = Arrow(start=[-13,6,0],end=[-5,0,0], color=RED
                       , stroke_width=20, tip_length=1)
        arrow6 = Arrow(start=[-13,-6,0],end=[-5,0,0], color=RED
                       , stroke_width=20, tip_length=1)
        self.play(Create(arrow5),Create(arrow6))

        text49 = Tex(
                    r"$y+2\lambda x=0$",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([4,3,0])
       
        text50 = Tex(
                    r"$2y+2\lambda z=0$",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([4,-3,0])
        self.play(Create(text49),Create(text50))

        text51 = Tex(
                    r"$y=-2\lambda x$",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([3,3,0])
       
        text52 = Tex(
                    r"$y=\lambda z$",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([3,-3,0])
        self.play(
            ReplacementTransform(text49,text51),
            ReplacementTransform(text50,text52)
        )
        self.wait(0.5)
        arrow7 = Arrow(start=[10,0,0],end=[17,0,0], color=RED
                       , stroke_width=20, tip_length=1)
        self.play(Create(arrow7))
        curve = CustomCurve(color=WHITE, stroke_width=20)
        
        # 添加到场景并播放动画
        self.play(Create(curve))
        text53 = Tex(
                    r"$\lambda=0$",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([30,5,0])
        text54 = Tex(
                    r"$\dfrac{2x}{z}=1$",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([30,-5,0])
        self.play(Create(text53),Create(text54))
        
        rectangle9=Rectangle(color=BLUE,fill_opacity=0.0,width=8,height=5
                            ,stroke_color=PURPLE,stroke_width=20)
        rectangle9.move_to([30,5,0])
        self.play(Create(rectangle9),self.camera.frame.animate.scale(1).move_to([0,-5,0]))
        text55 = Tex(
                    r"$\lambda=0$",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-30,-25,0])
        self.play(Create(text55))
        arrow8 = Arrow(start=[-25,-25,0],end=[-15,-25,0], color=RED
                       , stroke_width=20, tip_length=1)
        self.play(Create(arrow8))
        text56 = Tex(
                    r"$1.2.3.$",color=BLUE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-20,-23,0])
        self.play(Create(text56),self.camera.frame.animate.scale(1).move_to([0,-10,0]))
        curve1 = CustomCurve1(color=WHITE, stroke_width=20)
        
        # 添加到场景并播放动画
        self.play(Create(curve1))
        text59 = Tex(
                    r"$y=0$",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-2,-20,0])
        text57 = Tex(
                    r"$x+2z=0$",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-2,-25,0])
        text58 = Tex(
                    r"$y=0$",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-2,-30,0])
        self.play(Create(text58),Create(text57),Create(text59))

        rectangle10=Rectangle(color=BLUE,fill_opacity=0.0,width=12,height=4
                            ,stroke_color=PURPLE,stroke_width=20)
        rectangle10.move_to([-2,-20,0])
        rectangle11=Rectangle(color=BLUE,fill_opacity=0.0,width=12,height=4
                            ,stroke_color=PURPLE,stroke_width=20)
        rectangle11.move_to([-2,-25,0])
        self.play(Create(rectangle10),Create(rectangle11))

        
        


        curve2 = CustomCurve2(color=BLUE, stroke_width=20)
        
        # 添加到场景并播放动画
        self.play(Create(curve2))

        arrow9 = Arrow(start=[11,-15,0],end=[15,-15,0], color=RED
                       , stroke_width=20, tip_length=1)
        self.play(Create(arrow9))
        text59 = Tex(
                    r"$z^{2}=2$",color=BLUE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([20,-15,0])
        self.play(Create(text59))

        arrow10 = Arrow(start=[25,-15,0],end=[30,-15,0], color=RED
                       , stroke_width=20, tip_length=1)
        self.play(Create(arrow10))

        text60 = Tex(
                    r"$z=\pm \sqrt{2}$",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([37,-15,0])
        text61 = Tex(
                    r"$z=\pm \sqrt{2}$",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([37,-15,0])
        text620 = Tex(
                    r"$P_{1}\left( -2\sqrt{2},0,-\sqrt{2}\right) $",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([36.5,-12,0])
        text630 = Tex(
                    r"$P_{2}\left( 2\sqrt{2},0,\sqrt{2}\right) $",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([36.5,-18,0])
        self.play(Create(text60),Create(text61),self.camera.frame.animate.scale(1).shift(DOWN*5))

        self.play(
            FadeOut(arrow10),
            ReplacementTransform(text60,text620),
            ReplacementTransform(text61,text630)
        )


        self.wait(0.5)

        rectangle12=Rectangle(color=BLUE,fill_opacity=0.0,width=8,height=6
                            ,stroke_color=PURPLE,stroke_width=20)
        rectangle12.move_to([30,-5,0])
        self.play(Create(rectangle12),self.camera.frame.animate.scale(1).shift(DOWN*5))
        
        #######

        text55 = Tex(
                    r"$2x=z$",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-30,-40,0])
        self.play(Create(text55))
        text550 = Tex(
                    r"\text{(还需1个)}",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text55,DOWN,buff=1)
        self.play(Create(text550))
        arrow8 = Arrow(start=[-25,-40,0],end=[-15,-40,0], color=RED
                       , stroke_width=20, tip_length=1)
        self.play(Create(arrow8))
        text56 = Tex(
                    r"$1.2.3.$",color=BLUE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-20,-38,0])
        self.play(Create(text56))
        curve3 = CustomCurve3(color=WHITE, stroke_width=20)
        
        # 添加到场景并播放动画
        self.play(Create(curve3))
        text59 = Tex(
                    r"$y+\lambda z=0$",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-2,-35,0])
        text57 = Tex(
                    r"$5x+2\lambda y=0$",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-2,-40,0])
        text58 = Tex(
                    r"$y+2\lambda x=0$",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-2,-45,0])
        self.play(Create(text58),Create(text57),Create(text59))
        
        rectangle10=Rectangle(color=BLUE,fill_opacity=0.0,width=15,height=4
                            ,stroke_color=PURPLE,stroke_width=20)
        rectangle10.move_to([-2,-40,0])
        rectangle11=Rectangle(color=BLUE,fill_opacity=0.0,width=15,height=4
                            ,stroke_color=PURPLE,stroke_width=20)
        rectangle11.move_to([-2,-45,0])
        self.play(Create(rectangle10),Create(rectangle11))
        arrow8 = Arrow(start=[7,-40,0],end=[15,-40,0], color=RED
                       , stroke_width=20, tip_length=1)
        
        rectangle12=Rectangle(color=BLUE,fill_opacity=0.0,width=14,height=10
                            ,stroke_color=PURE_RED,stroke_width=20)
        rectangle12.move_to([3,0,0])
        self.play(Create(rectangle12))
        text59 = Tex(
                    r"$y^{2}=5x^{2}$",color=BLUE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([21,-40,0])
        self.play(
            Create(arrow8),
            
            ReplacementTransform(rectangle12,text59)
        )
        
        
        text60 = Tex(
                    r"$y=\pm \sqrt{5x}$",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([38,-40,0])
        
        text61 = Tex(
                    r"$y=\pm \sqrt{5x}$",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([38,-40,0])
        
        text62 = Tex(
                    r"$y=\pm \sqrt{5x}$",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([38,-40,0])
        
        text63 = Tex(
                    r"$y=\pm \sqrt{5x}$",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([38,-40,0])
        self.play(Create(text63),Create(text62),Create(text61),Create(text60))


        text64 = Tex(
                    r"$p_{3}\left( -1,-\sqrt{5},-2\right) $",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([36.5,-24,0])
        
        text65 = Tex(
                    r"$p_{4}\left( -1,\sqrt{5},-2\right) $",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([36.5,-30,0])
        
        text66 = Tex(
                    r"$p_{5}\left( 1,-\sqrt{5},2\right) $",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([36.5,-36,0])
        
        text67 = Tex(
                    r"$p_{6}\left( 1,\sqrt{5},2\right) $",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([36.5,-42,0])
        


        self.play(
            FadeOut(arrow9),
            ReplacementTransform(text60,text64),
            ReplacementTransform(text61,text65),
            ReplacementTransform(text62,text66),
            ReplacementTransform(text63,text67)
        )

        self.play(self.camera.frame.animate.scale(1.5).move_to([0,-7,0]))
        rectangle11=Rectangle(color=BLUE,fill_opacity=0.0,width=15,height=4
                            ,stroke_color=PURE_GREEN,stroke_width=20)
        rectangle11.move_to([-17.25,20,0])
        self.play(Create(rectangle11))
        text670 = Tex(
                    r"$u_{\min }=-5\sqrt{5}$",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-10,30,0])
        text680 = Tex(
                    r"$u_{\max }=5\sqrt{5}$",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([10,30,0])
        self.play(
            
            ReplacementTransform(text60,text670),
            ReplacementTransform(text61,text670),
            ReplacementTransform(text62,text670),
            ReplacementTransform(text63,text670),
            ReplacementTransform(text620,text680),
            ReplacementTransform(text630,text680),
            self.camera.frame.animate.scale(0.6).move_to([0,30,0])
        )

        self.wait(0.5)
        self.wait()